package com.example.bluetooth.ble.utils;

import android.os.Handler;
import android.util.Log;

import com.example.bluetooth.ble.manager.BluetoothControl;
import com.example.bluetooth.ble.service.BluetoothLeService;


/**
 * Created by HP on 2017/5/24.
 */

public class BluetoothControlUtil implements BluetoothControl.OnSendResultListener {

    private Handler mHandler;
    public static final String ACTION_READ_ID_BEGIN = "ReadIdBegin";
    public static final String ACTION_UNLOCK_BEGIN = "UnLockBegin";
    public static final String ACTION_WRITE_ID_BEGIN = "WriteIdBegin";
    public static final String ACTION_LOCK_BEGIN = "LockBegin";
    public static final String ACTION_CONNECT = "CONNECT";
    public static final String ACTION_ADCONNECT = "ADCONNECT";
    public static final String ACTION_DISCONNECT = "DISCONNECT";
    public static final String ACTION_UPDATE_KEY_BEGIN = "UpdateKeyBegin";
    public static final String ADDRESS = "ADDRESS";
    public static final String LOCK_ID = "LockId";
    public static final String LOCK_SAFE = "LockSafe";
    public static final String ZONE_KEY = "ZoneKey";
    public static final String LID = "Lid";
    public static final String ZONE_ID = "ZoneId";

    public static final int ZYDL = 101;
    public static final int SYJE = 102;
    public static final int DY = 103;
    public static final int DL = 104;
    public static final int GL = 105;
    public static final int GDCS = 106;
    public static final int SYYD = 107;
    public static final int BYYD = 108;
    public static final int J = 109;
    public static final int F = 110;
    public static final int P = 111;
    public static final int G = 112;
    public static final int READ_AMMETERING = 113;
    public static final int READ_AMMETER_RESULT = 114;

    private DataProcessUtil mDataProcessUtil;
    private BluetoothControl mBluetoothControl;

    private boolean connectState = false;//蓝牙连接状态
    private String currentAddress;//当前蓝牙MAC地址
    private String currentAmmeter = "834128040018";
    private String TAG=this.getClass().getSimpleName();

    public void setCurrentAmmeter(String ammeter) {
        this.currentAmmeter = ammeter;
    }

    public String getCurrentAmmeter() {
        return this.currentAmmeter;
    }

    public boolean getConnectState() {
        return connectState;
    }

    public void setConnectState(boolean state) {
        this.connectState = state;
    }

    public String getCurrentAddress() {
        return currentAddress;
    }

    public void setCurrentAddress(String currentAddress) {
        this.currentAddress = currentAddress;
    }

    private static volatile BluetoothControlUtil bluetoothControlUtil;

    public static BluetoothControlUtil getInstance() {
        if (null == bluetoothControlUtil) {
            synchronized (BluetoothControlUtil.class) {
                if (null == bluetoothControlUtil) {
                    bluetoothControlUtil = new BluetoothControlUtil();
                }
            }
        }
        return bluetoothControlUtil;
    }

    private byte[] lock_id;
    private byte[] lock_safe;

    public byte[] getLock_id() {
        return lock_id;
    }

    public void setLock_id(byte[] lock_id) {
        this.lock_id = lock_id;
    }

    public byte[] getLock_safe() {
        return lock_safe;
    }

    public void setLock_safe(byte[] lock_safe) {
        this.lock_safe = lock_safe;
    }

    protected BluetoothControlUtil() {
        mDataProcessUtil = DataProcessUtil.getInstance();
    }

    public void setBluetoothControl(BluetoothControl bluetoothControl) {
        this.mBluetoothControl = bluetoothControl;
        mBluetoothControl.setOnSendResultListener(this);
    }

    public Handler getmHandler() {
        return mHandler;
    }

    public void setmHandler(Handler mHandler) {
        this.mHandler = mHandler;
    }

    public void readAmmeterData(byte[] data, Handler lHandler) {
        setmHandler(lHandler);
        byte[] value = mDataProcessUtil.makePacket((byte) 0x3a, data);
        BluetoothLeService.getInstance().writeRXCharacteristic(value);
    }

    public void writeAmmeterData(byte command, byte[] data, Handler lHandler) {
        setmHandler(lHandler);
        byte[] value = mDataProcessUtil.makePacket(command, data);
        BluetoothLeService.getInstance().writeRXCharacteristic(value);
    }

    private String strings;
    private byte commond;
    private int count = 0;
    private String data;

    /**
     * 透传协议下发数据
     * @param data
     * @param handler
     */
    public void writeData(String data, Handler handler) {
        setmHandler(handler);
        if (data.length() <= 17 * 2 && data.length() > 0) {
            strings = data;
            if (strings.length() % 2 == 1) {
                strings = "0" + strings;
            }
            commond = (byte) 0xC0;
        } else if (data.length() > 17 * 2) {
            this.data = data;
            strings = data.substring(count * 17 * 2, (count + 1) * 17 * 2);
            commond = (byte) 0x80;
        }
        try {
            writeAmmeterData(commond, DataProcessUtil.getInstance().hexStr2Bytes(strings));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void writeAmmeterData(byte command, byte[] data) {
        byte[] value = mDataProcessUtil.makePacket1(command, data);
        BluetoothLeService.getInstance().writeRXCharacteristic(value);
    }

    @Override
    public void result(boolean result, boolean end) {
        Log.e("TAG", "result: result = " + result);
        Log.e("TAG", "result: end = " + end);
        if (result) {
            if (!end) {
                count++;
                if ((data.length() - 17 * 2 * count) <= 17 * 2 && (data.length() - 17 * 2 * count) > 0) {
                    strings = data.substring(count * 17 * 2, data.length());
                    if (strings.length() % 2 == 1) {
                        strings = "0" + strings;
                    }
                    commond = (byte) ((byte) 0x40 + count);
                    count = 0;
                } else if ((data.length() - 17 * 2 * count) > 17 * 2) {
                    strings = data.substring(count * 17 * 2, (count + 1) * 17 * 2);
                    commond = (byte) ((byte) 0x00 + count);
                }
                writeAmmeterData(commond, DataProcessUtil.getInstance().hexStr2Bytes(strings));
            } else {
                Log.e(TAG,"发送成功");
            }
        } else {
            writeAmmeterData(commond, DataProcessUtil.getInstance().hexStr2Bytes(strings));
        }
    }
}
